<?php 

class LsPanel
extends CWidget
{
	
	public $textarea_id;
	
	public $group_separator = ' &nbsp';
	public $button_separator = '';
	
	public $panel_class = 'panel_form';
	
	public $link_class = 'button';
	
	public $add = array();
	
	public $htmlOptions = array(
		'style' => 'background: #eaecea; margin-top: 2px;',
	);
	
	public static $buttons = array(
		'bold'	=> array(
			'click' => 'lsPanel.putTagAround({{textarea_id}},"b"); return false;',
			'img'	=> '{{cfg:path.static.skin}}/images/panel/bold_ru.gif',
			'label' => '{{lang:panel_b}}',
		),
		'italic'	=> array(
			'click' => 'lsPanel.putTagAround({{textarea_id}},"i"); return false;',
			'img'	=> '{{cfg:path.static.skin}}/images/panel/italic_ru.gif',
			'label' => '{{lang:panel_i}}',
		),
		'underline'	=> array(
			'click' => 'lsPanel.putTagAround({{textarea_id}},"u"); return false;',
			'img'	=> '{{cfg:path.static.skin}}/images/panel/underline_ru.gif',
			'label' => '{{lang:panel_u}}',
		),
		'strike'	=> array(
			'click' => 'lsPanel.putTagAround({{textarea_id}},"s"); return false;',
			'img'	=> '{{cfg:path.static.skin}}/images/panel/strikethrough.gif',
			'label' => '{{lang:panel_s}}',
		),
		'link'	=> array(
			'click' => 'lsPanel.putTagUrl({{textarea_id}},{{jsonlang:panel_url_promt}}); return false;',
			'img'	=> '{{cfg:path.static.skin}}/images/panel/link.gif',
			'label' => '{{lang:panel_url}}',
		),
		'quote'	=> array(
			'click' => 'lsPanel.putQuote({{textarea_id}}); return false;',
			'img'	=> '{{cfg:path.static.skin}}/images/panel/quote.gif',
			'label' => '{{lang:panel_quote}}',
		),
		'code'	=> array(
			'click' => 'lsPanel.putTagAround({{textarea_id}},"code"); return false;',
			'img'	=> '{{cfg:path.static.skin}}/images/panel/code.gif',
			'label' => '{{lang:panel_code}}',
		),
		'video'	=> array(
			'click' => 'lsPanel.putTagAround({{textarea_id}},"video"); return false;',
			'img'	=> '{{cfg:path.static.skin}}/images/panel/video.gif',
			'label' => '{{lang:panel_video}}',
		),
		'image'	=> array(
			'click'	=> 'showImgUploadForm(); return false;',
			'img'	=> '{{cfg:path.static.skin}}/images/panel/img.gif',
			'label' => '{{lang:panel_image}}',
		),
		'cut'	=> array(
			'click'	=> 'lsPanel.putText({{textarea_id}},"<cut>"); return false;',
			'img'	=> '{{cfg:path.static.skin}}/images/panel/cut.gif',
			'label' => '{{lang:panel_cut}}',
		),
		'h4'	=> array(
			'click'	=> 'lsPanel.putTagAround({{textarea_id}},"h4"); return false;',
			'img'	=> '{{tplurl:yii}}/images/panel/h4.gif',
			'label' => '{{lang:yii_panel_title_h4}}',
		),
		'h5'	=> array(
			'click'	=> 'lsPanel.putTagAround({{textarea_id}},"h5"); return false;',
			'img'	=> '{{tplurl:yii}}/images/panel/h5.gif',
			'label' => '{{lang:yii_panel_title_h5}}',
		),
		'h6'	=> array(
			'click'	=> 'lsPanel.putTagAround({{textarea_id}},"h6"); return false;',
			'img'	=> '{{tplurl:yii}}/images/panel/h6.gif',
			'label' => '{{lang:yii_panel_title_h6}}',
		),
		'ul'	=> array(
			'click'	=> 'lsPanel.putTagAround({{textarea_id}},"ul>\n  <li","li>\n</ul"); return false;',
			'img'	=> '{{tplurl:yii}}/images/panel/ul.gif',
			'label' => '{{lang:yii_panel_list_ul}}',
		),
		'ol'	=> array(
			'click'	=> 'lsPanel.putTagAround({{textarea_id}},"ol>\n  <li","li>\n</ol"); return false;',
			'img'	=> '{{tplurl:yii}}/images/panel/ol.gif',
			'label' => '{{lang:yii_panel_list_ol}}',
		),
		'li'	=> array(
			'click'	=> 'lsPanel.putTagAround({{textarea_id}},"li"); return false;',
			'img'	=> '{{tplurl:yii}}/images/panel/li.gif',
			'label' => '{{lang:yii_panel_list_li}}',
		),
			/*'tag'	=> array(
				'name'	=> 'b',
				'label' => 'LI',
				'htmlOptions' => array(
					'style' => 'display:inline-block; color:#5d849e; line-height:20px; vertical-align:top; font-width:14px; margin-top:1px',
				),
			),*/
	);
	
	public static $sets = array(
		'topic' => array(
			'h4', 'h5', 'h6',	// headers
			'separator',
			'ul', 'ol', 'li',	// lists
			'separator',
			'bold', 'italic', 'underline', 'strike',	// text style
			'separator',
			'link', 'quote', 'code',	// text type
			'separator',
			'video', 'image',	// media
			'separator',
			'cut',	// extra
			'separator',
		),
		'comment' => array(
			'bold', 'italic', 'underline', 'strike',	// text style
			'separator',
			'link', 'quote', 'code',	// text type
			'separator',
			'video', //'image',	// media
			'separator',
		),
		'talk' => array(
			'h4', 'h5', 'h6',	// headers
			'separator',
			'ul', 'ol', 'li',	// lists
			'separator',
			'bold', 'italic', 'underline', 'strike',	// text style
			'separator',
			'link', 'quote', 'code',	// text type
			'separator',
			'video', 'image',	// media
			'separator',
		),
		'page' => array(
			'h4', 'h5', 'h6',	// headers
			'separator',
			'ul', 'ol', 'li',	// lists
			'separator',
			'bold', 'italic', 'underline', 'strike',	// text style
			'separator',
			'link', 'quote', 'code',	// text type
			'separator',
			'video', 'image',	// media
			'separator',
		),
	);
	
	public $set = 'comment';
	
	public $vars = array();
	
	
	public function getButtonNames($set){
		$names = isset(self::$sets[$set])
			? self::$sets[$set]
			: array()
		;
		
		foreach($this->add as $aName){
			if(is_array($aName)){
				$name = $aName[0];
				$pos = $aName[1];
				if(isset($names[$pos])){
					$names = array_merge(
						array_slice($names, 0, $pos),
						array($name),
						array_slice($names, $pos)
					);
				}else{
					$names[] = $name;
				}
			}else{
				$names[] = $aName;
			}
		}
		
		return $names;
	}
	
	public function getButtonsByNames($button_names){
		$buttons = array();
		foreach($button_names as $button_name){
			if($button_name == 'separator'){
				$buttons[] = array('separator' => true);
			}elseif(isset(self::$buttons[$button_name])){
				$buttons[] = self::$buttons[$button_name];
			}
		}
		return $buttons;
	}
	
	public function replaceVars($str){
		if(!preg_match_all('#\{\{([^\{\}]+)\}\}#Umsu', $str, $tt, PREG_SET_ORDER)){
			return $str;
		}
		$replace = array();
		foreach($tt as $t){
			$match = $t[0];
			$value = $match;
			$vars = explode(':', $t[1]);
			if(count($vars) > 1){
				if(method_exists($this, $method = 'callback'.$vars[0])){
					$value = $this->$method($vars);
				}
			}elseif(array_key_exists($vars[0], $this->vars)){
				$value = $this->vars[$vars[0]];
			}
			$replace[$match] = $value;
		}
		return strtr($str, $replace);
	}
	
	
	public function renderButton($button){
		if(!empty($button['separator'])){
			return $this->group_separator;
		}
		$img = $this->replaceVars(@$button['img']);
		$label = $this->replaceVars($button['label']);
		$click = $this->replaceVars($button['click']);
		
		if(empty($img)){
			$htmlOptions = isset($button['tag']['htmlOptions'])
				? $button['tag']['htmlOptions']
				: array()
			;
			$content = CHtml::tag(
				$button['tag']['name'],
				array(
					'title' => $label,
				) + $htmlOptions,
				@$button['tag']['label']
			);
		}else{
			$content = CHtml::image(
				$img,
				$label,
				array(
					'title' => $label,
				)
			);
		}
		
		return CHtml::link(
			$content,
			'#',
			array(
				'onclick' => $click,
				'class' => $this->link_class,
				'style' => 'text-decoration:none',
			)
		);
	}
	
	public function callbackLang($vars){
		return Yii::E()->Lang_Get($vars[1]);
	}
	
	public function callbackJsonlang($vars){
		return json_encode(Yii::E()->Lang_Get($vars[1]));
	}
	
	public function callbackCfg($vars){
		return Config::Get($vars[1]);
	}
	
	public function callbackJsoncfg($vars){
		return json_encode($this->callbackCfg($vars));
	}
	
	public function callbackTplpath($vars){
		return Yii::app()->getTemplatePath($vars[1]);
	}
	
	public function callbackTplurl($vars){
		return Yii::app()->getTemplateUrl($vars[1]);
	}
	
	public function init(){
		$this->vars['textarea_id'] = json_encode($this->textarea_id);
	}
	
	public function run(){
		
		$button_names = $this->getButtonNames($this->set);
		if(!$button_names){
			return;
		}
		
		$buttons = $this->getButtonsByNames($button_names);
		
		$buttons_strs = array();
		foreach($buttons as $button){
			$buttons_strs[] = $this->renderButton($button);
		}
		
		$htmlOpt = $this->htmlOptions ? $this->htmlOptions : array();
		if($this->panel_class){
			$htmlOpt['class'] = $this->panel_class;
		}
		
		echo CHtml::tag(
			'div',
			$htmlOpt,
			join($this->button_separator, $buttons_strs)
		);

	}
	
}

?>